package com.example.mywanandroid.ui.nav.upData;

import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mywanandroid.BR;
import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.UpDataPlan;
import com.example.mywanandroid.databinding.UpdataFragmentBinding;
import com.example.mywanandroid.ui.activity.web.DetailsActivity;
import com.example.mywanandroid.ui.adapter.CommonAdapter;

public class UpDataFragment extends BaseFragment<UpdataFragmentBinding, UpDataViewModel> {

    private CommonAdapter<UpDataPlan> commonAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.updata_fragment;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(UpDataViewModel.class);
    }

    @Override
    protected void bindViewModel() {
        mDataBinding.setViewModel(mViewModel);
        mDataBinding.setFragment(this);
    }

    @Override
    protected void init() {
        addObserve();
        initRecycle();
    }

    private void addObserve() {
        mViewModel.getPlanList().observe(this, updatePlans ->
                commonAdapter.onItemDatasChanged(updatePlans)
        );
    }

    private void initRecycle() {
        commonAdapter = new CommonAdapter<UpDataPlan>(R.layout.item_update_plan, BR.updatePlan);
        mDataBinding.recyclerViewPlan.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.recyclerViewPlan.setAdapter(commonAdapter);
    }


    public void startProject() {
        DetailsActivity.start(getContext(), "https://gitee.com/ShenZhiHao/my-wan-android.git");
    }

    public void showUpdatePlan() {
        if (mDataBinding.recyclerViewPlan.getVisibility() == View.VISIBLE) {
            mDataBinding.recyclerViewPlan.setVisibility(View.GONE);
            mDataBinding.ivPlan.setImageResource(R.mipmap.ic_chevron_down);
        } else {
            mDataBinding.recyclerViewPlan.setVisibility(View.VISIBLE);
            mDataBinding.ivPlan.setImageResource(R.mipmap.ic_chevron_up);
        }
    }

    public void showUpdateRecord() {
        if (mDataBinding.llRecord.getVisibility() == View.VISIBLE) {
            mDataBinding.llRecord.setVisibility(View.GONE);
            mDataBinding.ivRecord.setImageResource(R.mipmap.ic_chevron_down);
        } else {
            mDataBinding.llRecord.setVisibility(View.VISIBLE);
            mDataBinding.ivRecord.setImageResource(R.mipmap.ic_chevron_up);
        }
    }

}
